<?php
/**
 *
 * @author titorov
 * @version 
 */
require_once 'Zend/View/Interface.php';

/**
 * NavBar helper
 *
 * @uses viewHelper Zend_View_Helper
 */
// Адаптировано из http://www.tigir.com/paging.htm#php_paging_class
class Zend_View_Helper_NavBar {
	
	/**
	 * @var Zend_View_Interface 
	 */
	public $view;
	
	private $page_size = 10;
	private $link_padding = 5;
	private $next_page_text = '&gt;';
	private $prev_page_text = '&lt;';
	private $page_var = 'page';
	private $urlALiasl = '';
	
	private $total_rows;
	private $total_pages;
	private $cur_page;
	/**
	 * 
	 */
	public function navBar($total_rows, $page_size = 10, $urlALias = '', $prev_page_text = '&lt;', $next_page_text = '&gt;', $link_padding = 5, $page_var = 'page') {
		$this->page_var = $page_var;
		$this->cur_page = isset ( $_GET [$this->page_var] ) && ( int ) $_GET [$this->page_var] > 0 ? ( int ) $_GET [$this->page_var] : 1;
		$this->page_size = abs ( ( int ) $page_size );
		$this->link_padding = abs ( ( int ) $link_padding );
		$this->urlALias = $urlALias;
		$this->set_total_rows ( $total_rows );
		$this->next_page_text = $next_page_text;
		$this->prev_page_text = $prev_page_text;
		return null;
	}
	
	public function get_page_links() {
		if (! isset ( $this->total_pages ))
			return '';
		
		$page_link_list = array ();
		
		$start = $this->cur_page - $this->link_padding;
		
		if ($start < 1)
			$start = 1;
		
		$end = $this->cur_page + $this->link_padding - 1;
		
		if ($end > $this->total_pages)
			$end = $this->total_pages;
		
		while ( $end - $start + 1 >= $this->link_padding ) {
			$start ++;
			if (($end - $start + 1 >= $this->link_padding)) {
				$end --;
			}
		}
		
		if ($start > 1)
			$page_link_list [] = $this->get_page_link ( $start - 1, $start - 2 > 0 ? '...' : '' );
		
		for($i = $start; $i <= $end; $i ++)
			$page_link_list [] = $this->get_page_link ( $i );
		
		if ($end + 1 < $this->total_pages)
			$page_link_list [] = $this->get_page_link ( $end + 1, $end + 2 == $this->total_pages ? '' : '...' );
		
		if ($end + 1 <= $this->total_pages)
			$page_link_list [] = $this->get_page_link ( $this->total_pages );
		
		return $page_link_list;
	}
	
	public function get_next_page_link() {
		return isset ( $this->total_pages ) && $this->cur_page < $this->total_pages ? $this->get_page_link ( $this->cur_page + 1, $this->next_page_text, ' class="arrow"' ) : '';
	}
	
	public function get_prev_page_link() {
		return isset ( $this->total_pages ) && $this->cur_page > 1 ? $this->get_page_link ( $this->cur_page - 1, $this->prev_page_text, ' class="arrow"' ) : '';
	}
	
	public function get_last_page_link($last_page_text) {
		return isset ( $this->total_pages ) && $this->cur_page < $this->total_pages ? $this->get_page_link ( $this->total_pages, $last_page_text, ' class="arrow"' ) : '';
	}
	
	public function get_first_page_link($first_page_text) {
		return isset ( $this->total_pages ) && $this->cur_page > 1 ? $this->get_page_link ( 1, $first_page_text, ' class="arrow"' ) : '';
	}
	
	private function set_total_rows($total_rows) {
		$this->total_rows = $total_rows;
		
		if ($this->page_size !== 0)
			$this->total_pages = ceil ( $this->total_rows / $this->page_size );
		
		if ($this->cur_page > $this->total_pages) {
			$this->cur_page = $this->total_pages;
		}
	}
	
	private function get_page_link($page, $text = '', $style = '') {
		if (! $text)
			$text = $page;
		
		if ($page != $this->cur_page) {
			$reg = '/((&|^)' . $this->page_var . '=)[^&#]*/';
			$url = '?' . (preg_match ( $reg, $_SERVER ['QUERY_STRING'] ) ? preg_replace ( $reg, '${1}' . $page, $_SERVER ['QUERY_STRING'] ) : ($_SERVER ['QUERY_STRING'] ? $_SERVER ['QUERY_STRING'] . '&' : '') . $this->page_var . '=' . $page);
			//$url = (get_magic_quotes_gpc()) ? stripslashes($url) : $url;
			if ($page == 1) {
				$url = str_replace ( '?' . $this->page_var . "=1", '', $url );
				$url = str_replace ( '&' . $this->page_var . "=1", '', $url );
			}
			return '<a href="' . $this->urlALias . $url . '"' . $style . '>' . $text . '</a>';
		}
		return $text;
	}
	
	/**
	 * Sets the view field 
	 * @param $view Zend_View_Interface
	 */
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
	}
}
